<?php

/**
 * @version     1.0.0
 * @package     com_domainchecker
 * @copyright   Copyright (C) 2014. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 * @author      Quang <quang4it@yahoo.com> - http://quang.com
 */
// No direct access.
defined('_JEXEC') or die;

ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once JPATH_COMPONENT . '/controller.php';
require_once JPATH_ADMINISTRATOR . '/includes/whois/whois.php';

/**
 * Domainchecklists list controller class.
 */
class DomaincheckerControllerDomainchecklists extends DomaincheckerController {

    /**
     * Proxy for getModel.
     * @since	1.6
     */
    public function &getModel($name = 'Domainchecklists', $prefix = 'DomaincheckerModel') {
        $model = parent::getModel($name, $prefix, array('ignore_request' => true));
        return $model;
    }

    function check() {
//        JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
        $app = new App();
        $model = $app->getModel("types", "DomaincheckerModel", false, 2);
        $list = $model->getItems();
        $input = JFactory::getApplication()->input;
        $domain = $input->getVar("domain");
        $match = $this->checkDomain($domain);
//        if (is_array($list) && count($list)) {
//            foreach ($list as $key => $value) {
//                $match = $this->checkDomain($domain, $value->type);
//                echo $match;
//            }
//        }
    }

    function checkList($domain, $server) {
        $ch = curl_init();
        $link = 'http://whoiz.herokuapp.com/lookup.json?url=' . $domain;
        $pr = parse_url($link);

        $header = array('GET /1575051 HTTP/1.1',
            "Host: {$pr['host']}",
            'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language:en-US,en;q=0.8',
            'Cache-Control:max-age=0',
            'Connection:keep-alive',
            'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36',
        );

        curl_setopt($ch, CURLOPT_URL, $link);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
        curl_setopt($ch, CURLOPT_COOKIESESSION, true);

        curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
        curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $result = curl_exec($ch);
        curl_close($ch);
        libxml_use_internal_errors(true);
        $doc = new DOMDocument();
        $html = $doc->loadHTML($result);
        libxml_use_internal_errors(false);
        $xpath = new DOMXPath($doc);
        $quote = $xpath->query('//body');
//        echo "<pre>";
        echo $a = str_replace("\\n", "<br />", $result);
        echo $a = str_replace("\\n", "<br />", $a);
//echo "</pre>";
        die;
    }

    function checkDomain($domain = NULL, $server = NULL, $findText = NULL) {
        $domain = new Whois($domain);

        $whois_answer = $domain->htmlInfo();

        if ($domain->isAvailable()) {
            echo "Domain is available\n";
        } else {
            echo "Domain is registered\n";
        }
        
        echo "<div class='rschkdomain'>";
        print_r ($whois_answer);
        echo "</div>";
        die;
    }

    function getwhois($domain, $tld) {

        $whois = new Whois();
        if (!$whois->ValidDomain($domain . $tld)) {
            return 'Sorry, the domain is not valid or not supported.';
        }

        if ($whois->Lookup($domain . $tld)) {
            return $whois->GetData(1);
        } else {
            return 'Sorry, an error occurred.';
        }
    }

}
